package picol.view.utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import picol.view.exception.SaveException;

/**
 *
 * @author Aurélien Marcel - Julien Sanderson
 */
public class PicolFileChooser extends JFileChooser {

    private static final String DEFAULT_FILE_PATH = "./test/jeux/";
    private static final String DEFAULT_AUTHORIZED_EXTENSION = ".picol";
    private static final String DEFAULT_AUTHORIZED_TEXT_EXTENSION = "Fichier Picol (*.picol)";

    public PicolFileChooser() {
        super(DEFAULT_FILE_PATH);
        SimpleFileFilter fileFilterPicol = new SimpleFileFilter(DEFAULT_AUTHORIZED_TEXT_EXTENSION, DEFAULT_AUTHORIZED_EXTENSION);
        this.addChoosableFileFilter(fileFilterPicol);
    }

    public PicolFileChooser(String _defaultPath, String _textAuthorized, String _extensionAuthorized) {
        super(_defaultPath);
        SimpleFileFilter fileFilterPicol = new SimpleFileFilter(_textAuthorized, _extensionAuthorized);
        this.addChoosableFileFilter(fileFilterPicol);
    }

    public void saveFile(String _filePath, String _content) throws SaveException {
        File file = new File(_filePath);
        StringTokenizer str = new StringTokenizer(file.getName(), ".");
        String fileName = file.getParent() + "/" + str.nextToken() + DEFAULT_AUTHORIZED_EXTENSION;
        String content = _content;
        try {
            WriteFile filInter = new WriteFile(fileName);
            filInter.writeFileInter(content);
            filInter.close();
        } catch (Exception e) {
            throw new SaveException();
        }
    }

    public String saveFileAs(String _content) throws SaveException {
        int returnVal = this.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = this.getSelectedFile();
            StringTokenizer str = new StringTokenizer(file.getName(), ".");
            String fileName = file.getParent() + "/" + str.nextToken() + DEFAULT_AUTHORIZED_EXTENSION;
            String content = _content;
            try {
                WriteFile filInter = new WriteFile(fileName);
                filInter.writeFileInter(content);
                filInter.close();
            } catch (Exception e) {
                throw new SaveException();
            }
            return fileName;
        }
        return null;
    }

    public String selectFile() {
        int returnVal = this.showOpenDialog(null);
        String returnFile = null;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return this.getSelectedFile().getPath();
        }
        return returnFile;
    }

    public String openFile(String _path) throws FileNotFoundException {
        String content = "";
        Scanner scanner = new Scanner(new File(_path));
        while (scanner.hasNextLine()) {
            content += scanner.nextLine() + "\n";
        }
        return content;
    }
}
